package com.yanqu.road.server.cmd.activity.huashigang;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.huashigang.HuaShiGangProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.huashigang.HuaShiGangModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.huashigang.HuaShiGangMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_HUA_SHI_GANG_SHEN_XING, desc = "神行千里")
public class ShenXingQianLiCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        HuaShiGangProto.ShenXingQianLiReqMsg reqMsg = HuaShiGangProto.ShenXingQianLiReqMsg.parseFrom(packet.getBytes());
        ret = HuaShiGangMgr.getActivityInfoRet(player);
        if (ret != 0) {
            return;
        }

        // 活动期间内才能操作
        ActivityInfo activityInfo = HuaShiGangMgr.getActivityInfo();
        if (!ActivityMgr.activityInTime(activityInfo)) {
            ret = GameErrorCode.E_HUA_SHI_GAG_NOT_IN_FIGHT_TIME;
            return;
        }

        if (!HuaShiGangMgr.inOperateTime(activityInfo)) {
            ret = GameErrorCode.E_HUASHIGANG_NOT_OPERATE_TIME;
            return;
        }

        if (reqMsg.getType() !=0 && reqMsg.getType() != 1) {
            ret = GameErrorCode.E_ERROR_PARAMETER;
            return;
        }

        HuaShiGangProto.CrossShenXingQianLiReqMsg.Builder builder = HuaShiGangProto.CrossShenXingQianLiReqMsg.newBuilder();
        builder.setActivityId(activityInfo.getActivityId());
        builder.setPlayerBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.getUserBaseInfo(player.getUserId(), GameServer.getInstance().getServerId())));
        builder.setTotalAbility(player.getModule(HuaShiGangModule.class).getPatronsAbility());
        builder.setType(reqMsg.getType());
        player.sendPacket(CrossProtocol.C2_HUA_SHI_GANG_SHEN_XING, builder);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            HuaShiGangProto.ShenXingQianLiRespMsg.Builder builder = HuaShiGangProto.ShenXingQianLiRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_HUA_SHI_GANG_SHEN_XING, builder);
        }
    }
}
